package com.minyifang.wcspf.work.ui.user.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.MakeToken;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.ui.user.model.CaptchaBean;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.TimeStampBean;
import com.minyifang.wcspf.work.ui.user.view.RegisterView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/14.
 */
public class RegisterPresenter extends BasePresenter<RegisterView> {
    public void getCaptchaUrl(String strPhone) {

        Map<String, Object> maps = new HashMap<>();
        maps.put("mobile", strPhone);
        maps.put("type", 0);
        JSONObject jsonData = Constants.getJsonData(maps, null);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCaptcha(requestBody), captchaBeanHttpRespond -> getView().showCaptchaUrl(captchaBeanHttpRespond));
    }

    public void sendSMS(String phone, String captcha) {

        Map<String, Object> maps = new HashMap<>();
        maps.put("mobile", phone);
        maps.put("piccode", captcha);
        JSONObject jsonData = Constants.getJsonData(maps, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getSmsCode(requestBody), httpRespond -> getView().sendSMSRes(httpRespond));
    }

    public void register(final String phone, final String pwd, final String captcha) {

        final JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getTimeStamps(requestBody).flatMap((Function<HttpRespond<TimeStampBean>, ObservableSource<HttpRespond>>) timeStampBeanHttpRespond -> {
            LogUtil.e(timeStampBeanHttpRespond.data.toString());

            Map<String, Object> maps = new HashMap<>();
            maps.put("username", phone);
            maps.put("password", SafeUtils.getEncryptPwd(pwd));
            maps.put("ticksid", timeStampBeanHttpRespond.data.id);
            maps.put("code", captcha);
            maps.put("storeId", UserManager.getInstance().getStoreId());

            JSONObject data = Constants.getJsonData(maps, MakeToken.getToken(phone, pwd, timeStampBeanHttpRespond.data.id, timeStampBeanHttpRespond.data.val));

            RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), data.toString());
            return RetrofitHelper.getInstance().getService().register(requestBody1);
        }), httpRespond -> getView().registerRes(httpRespond));


    }

    public void getProtocol() {
        JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getProtocal(requestBody), response -> getView().showProtocol(response));
    }
}
